﻿#region itdc

/************************************************************************
*Copyright(C) 2015 by qsgy
 *All rights reserved. 
 *FileName:     $safeitemname$
 *Author:       oyh
 *Version:      
 *UnityVersion：
 *Date:   $time$      
 *Description:    
 *History: 
************************************************************************/
#endregion
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Build : MonoBehaviour {
    #region 1
    public GameObject workP;
    public Material[] matertials;
    public Material currentM;
    public Transform currentPice;
    public Transform clip;
    public Hold hold;
    public Alarm alert;
    public int needBuild = 0;
    #endregion

    private void Update()
    {
        lock (this)
        {
            if (needBuild>0)
            {
                needBuild--;
                BuildWorkPiece();
            }
        }
    }

    //生成物块
    public void BuildWorkPiece()
    {
       GameObject go=  GameObject.Instantiate(workP,transform);
        currentPice = go.transform;
        go.transform.localPosition = new Vector3(0, 0, 0);
        foreach (Transform i in go.transform)
        {
            i.GetComponent<Renderer>().material = currentM;
        }
       
    }

    /// <summary>
    /// 设置新生成的物块颜色, 0,1,2,3分别是蓝绿红黄
    /// </summary>
    /// <param name="i"></param>
    public void SetColor(int i)
    {
        currentM = matertials[i];
    }

    public void Reset()
    {
        SceneManager.LoadScene(0);
    }


    private IEnumerator Start()
    {
        yield return 10;
        yield return 10;
        yield return 10;

        while (true)
        {
             // hold.CanHold();
            //  hold.Isholding();
             // alert.IsAlert();
           // Application.ExternalCall("GetClipPos",clip.position.x, clip.position.y, clip.position.z);
            if (currentPice!=null)
            {
                // Application.ExternalCall("GetPicePos", currentPice.position.x, currentPice.position.y, currentPice.position.z);

                ByteBuffer bf = ByteBuffer.Allocate(24);
                bf.WriteShort(22);
                bf.WriteUTF("position");
                bf.WriteFloat(currentPice.position.x);
                bf.WriteFloat(currentPice.position.y);
                bf.WriteFloat(currentPice.position.z);
                SocketFace.instance.SendBuf(bf);
               // print("pic");
            }
            yield return new WaitForSeconds(0.2f);
        }
    }
}
